package leetcode;

public class CaptureForts {
    public static void main(String[] args) {
        CaptureForts captureForts = new CaptureForts();
        System.out.println(captureForts.captureForts(new int[]{1, 0, 0, -1, 0, 0, -1, 0, 0, 1}));
    }

    public int captureForts(int[] forts) {
        int n = forts.length;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (forts[i] == 1) {
                int cnt = 0;
                for (int j = i + 1; j < n; j++) {
                    if (forts[j] == 0) {
                        cnt++;
                    } else {
                        if (forts[j] == -1) {
                            ans = Math.max(ans, j - i - 1);
                        }
                        break;
                    }
                }
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            if (forts[i] == 1) {
                int cnt = 0;
                for (int j = i - 1; j >= 0; j--) {
                    if (forts[j] == 0) {
                        cnt++;
                    } else {
                        if (forts[j] == -1) {
                            ans = Math.max(ans, i - j - 1);
                        }
                        break;
                    }
                }
            }
        }
        return ans;
    }
}
